package com.kitty.game.luoshu;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.enter.FiedValue;
import com.kitty.game.fight.service.CountService;
import com.kitty.game.fuling.FulingInfo;
import com.kitty.game.look.LookController;
import com.kitty.game.pet.model.Pet;
import com.kitty.game.pet.service.PetService;
import com.kitty.game.role.model.Role;
import com.kitty.game.team.message.RespMsg;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.message.Message;
import com.kitty.mina.message.MessagePusher;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class LuoshuController {
  Logger logger = LoggerFactory.getLogger(LookController.class);
  
  static CountService countService = (CountService)SpringUtils.getBean(CountService.class);
  
  @Autowired
  protected PetService petService;
  
  @RequestMapping
  public void reqLuoshuData(Role role, ReqLuoshuData reqLuoshuData) {
    Respreqluoshu respreqluoshu;
    int exp = 72000;
    int xiaolv = 50;
    MessagePusher.pushMessage(role, (Message)new RespMsg("伏妖经验每日限领一次！"));
    if (role.isIslingqufuyao()) {
      role.setFuyaoExp(0);
      respreqluoshu = setLuoshuData(role, 0, xiaolv);
    } else {
      respreqluoshu = setLuoshuData(role, exp, xiaolv);
    } 
    role.setFuyaoExp(exp);
    role.setFuyaoXiaolv(xiaolv);
    Updatejingyan updateJingyan = new Updatejingyan();
    ArrayList<FiedValue> fiedValues = new ArrayList<>();
    fiedValues.add(new FiedValue(31, 2, Short.valueOf(role.getLuoshuLevel())));
    fiedValues.add(new FiedValue(25, 3, Integer.valueOf(role.getLuoshuExp())));
    updateJingyan.setList(fiedValues);
    role.save();
    MessagePusher.pushMessage(role, (Message)updateJingyan);
    MessagePusher.pushMessage(role, (Message)respreqluoshu);
  }
  
  @RequestMapping
  public void setGetLuoshuJingyan(Role role, getLuoshuJingyan getLuoshuJingyan1) {
    RespMsg respInviteMsg = new RespMsg();
    Updatejingyan updateJingyan = new Updatejingyan();
    ArrayList<FiedValue> fiedValues = new ArrayList<>();
    fiedValues.add(new FiedValue(31, 2, Short.valueOf(role.getLuoshuLevel())));
    fiedValues.add(new FiedValue(25, 3, Integer.valueOf(role.getLuoshuExp() + role.getFuyaoExp())));
    role.setLuoshuExp(role.getLuoshuExp() + role.getFuyaoExp());
    updateJingyan.setList(fiedValues);
    role.setFuyaoExp(0);
    role.setIslingqufuyao(true);
    Respreqluoshu respreqluoshu = setLuoshuData(role, 0, role.getFuyaoXiaolv());
    role.save();
    respInviteMsg.setMsg("今日伏妖经验领取成功，每日仅限一次！");
    MessagePusher.pushMessage(role, (Message)respreqluoshu);
    MessagePusher.pushMessage(role, (Message)respInviteMsg);
    MessagePusher.pushMessage(role, (Message)updateJingyan);
  }
  
  @RequestMapping
  public void LuoshuUpdate(Role role, LuoshuUpdate luoshuUpdate) {
    int level = role.getLuoshuLevel();
    Updatejingyan updateJingyan = new Updatejingyan();
    int needexp = FulingInfo.luoshudata[level];
    if (role.getLuoshuExp() < needexp)
      return; 
    int exp = role.getLuoshuExp() - needexp;
    if ((level + 1) % 5 == 0) {
      int index = (level + 1) / 5 % 3;
      role.setDefense(role.getMaxDefense());
      role.setSpeed(role.getMaxSpeed());
      if (index == 1) {
        role.setLuoshuDefense(role.getLuoshuDefense() + 380);
        role.setDefense(role.getDefense() + 380);
      } else if (index == 2) {
        role.setLuoshuSpeed(role.getLuoshuSpeed() + 30);
      } else {
        role.setLuoshuMagpower(role.getLuoshuMagpower() + 105);
        role.setLuoshumPhypower(role.getLuoshumPhypower() + 190);
      } 
    } 
    ArrayList<FiedValue> fiedValues = new ArrayList<>();
    fiedValues.add(new FiedValue(31, 2, Integer.valueOf(level + 1)));
    fiedValues.add(new FiedValue(25, 3, Integer.valueOf(exp)));
    updateJingyan.setList(fiedValues);
    role.setLuoshuExp(exp);
    role.setLuoshuLevel((short)(role.getLuoshuLevel() + 1));
    role.save();
    MessagePusher.pushMessage(role, (Message)updateJingyan);
    SpringUtils.getRoleService().sendExtraAttribute(role);
    Collection<Pet> pets = role.getPetBox().getAllPets().values();
    for (Pet pet : pets)
      this.petService.refreshPetInfo(role, pet); 
  }
  
  public Respreqluoshu setLuoshuData(Role role, int exp, int xiaoLv) {
    Respreqluoshu respreqluoshu = new Respreqluoshu();
    respreqluoshu.setGuanka((short)1);
    respreqluoshu.setType((byte)0);
    respreqluoshu.setCguanka((short)2);
    respreqluoshu.setMonsterData(getfuyaoMonster(role));
    respreqluoshu.setListshenjiang(getFuyaoShenjiang(role));
    respreqluoshu.setJingyan(exp);
    respreqluoshu.setFf1(-1);
    respreqluoshu.setFf2(-1);
    respreqluoshu.setDangqianxiaolv(xiaoLv);
    respreqluoshu.setTargetCount((byte)0);
    respreqluoshu.setUnkonwnint(1601996942);
    respreqluoshu.setIsok((byte)0);
    return respreqluoshu;
  }
  
  public List<MonsterData> getfuyaoMonster(Role role) {
    ArrayList<MonsterData> arrayList = new ArrayList<>();
    MonsterData monsterData1 = new MonsterData();
    MonsterData monsterData2 = new MonsterData();
    monsterData1.setName("魔猪精");
    monsterData1.setMagpower(6208);
    monsterData1.setPhypower(1080);
    monsterData1.setSpeed(960);
    monsterData1.setDefense(0);
    monsterData1.setShuxing5(300);
    monsterData1.setShuxing6(0);
    monsterData1.setShuxing7(0);
    monsterData1.setShuxing8(0);
    monsterData1.setShuxing9(0);
    monsterData1.setShuxing10(0);
    monsterData1.setShuxing11(0);
    monsterData1.setSkillname("物攻");
    monsterData2.setName("魔猪精");
    monsterData2.setMagpower(6208);
    monsterData2.setPhypower(1080);
    monsterData2.setSpeed(960);
    monsterData2.setDefense(0);
    monsterData2.setShuxing5(300);
    monsterData2.setShuxing6(0);
    monsterData2.setShuxing7(0);
    monsterData2.setShuxing8(0);
    monsterData2.setShuxing9(0);
    monsterData2.setShuxing10(0);
    monsterData2.setShuxing11(0);
    monsterData2.setSkillname("物攻");
    arrayList.add(monsterData1);
    arrayList.add(monsterData2);
    return arrayList;
  }
  
  public List<ShenjiangData> getFuyaoShenjiang(Role role) {
    ArrayList<ShenjiangData> arrayList = new ArrayList<>();
    ShenjiangData ShenjiangData = new ShenjiangData();
    ShenjiangData.setName("杨戬");
    ShenjiangData.setMagpower(6208);
    ShenjiangData.setPhypower(1080);
    ShenjiangData.setSpeed(960);
    ShenjiangData.setDefense(0);
    ShenjiangData.setShuxing5(300);
    ShenjiangData.setShuxing6(0);
    ShenjiangData.setShuxing7(0);
    ShenjiangData.setShuxing8(0);
    ShenjiangData.setShuxing9(0);
    ShenjiangData.setShuxing10(0);
    ShenjiangData.setSkillname("物攻");
    ShenjiangData.setIschuzhan((byte)1);
    ShenjiangData.setPos((byte)1);
    arrayList.add(ShenjiangData);
    return arrayList;
  }
}
